<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>黑马旅游网-登录</title>
	<script src="js/jquery-3.3.1.js"></script>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/login.css">
    <script src="js/vue.js"></script>
    <script src="js/axios-0.19.0.js"></script>
</head>

<body>
<!--引入头部-->
<div id="header">
</div>
<!-- 头部 end -->
<div id="app">
    <section id="login_wrap">
        <div class="fullscreen-bg" style="background: url(images/login_bg.png);height: 532px;">

        </div>
        <div class="login-box">
            <div class="title">
                <img src="images/login_logo.png" alt="">
                <span>欢迎登录黑马旅游账户</span>
            </div>
            <div class="login_inner">
                <!--登录错误提示消息-->
                <div id="errorMsg" class="alert alert-warning alert-dismissible" :style="{color:infoColor}">{{msg}}</div>
                <form id="loginForm">
                    <input name="username" type="text" placeholder="请输入账号" ref="username"  v-model="user.username" autocomplete="off">
                    <input name="password" type="password" placeholder="请输入密码" ref="password" v-model="user.password" autocomplete="off">
                    <div class="verify">
                        <input name="check" type="text" placeholder="请输入验证码" ref="smsCode" v-model="user.smsCode" autocomplete="off">
                        <button type="button" class="btn btn-success btn-sm" @click="sendSms" :disabled="btnDisabled"><span class="glyphicon glyphicon-phone"/>{{btnText}}</button>
                    </div>
                    <div class="submit_btn">
                        <button type="button" id="btnLogin" @click="login">登录</button>
                        <div class="auto_login">
                            <input type="checkbox" name="" class="checkbox">
                            <span>自动登录</span>
                        </div>
                    </div>
                </form>
                <div class="reg">没有账户？<a href="register.html">立即注册</a></div>
            </div>
        </div>
    </section>
</div>
<!--引入尾部-->
<div id="footer">
</div>
<script src="js/include.js"></script>
<script>
    new Vue({
            el:"#app",
            data:{
                    msg:"输入用户名和密码", //提示信息的文本
                    infoColor:"black", //提示信息的颜色
                    user:{
                        username:"",
                        password:"",
                        smsCode:""
                    },
                    btnDisabled:false ,//发送短信的按钮的可用性,默认是可用的
                    btnText:"发送手机验证码"
            },
            methods:{
                sendSms(){
                    //1.用户名非空校验
                    if(this.user.username){

                        //2.校验通过发送异步请求，发送短信验证码，传递用户名给后端
                        axios.get(`user/loginSendMessage?username=${this.user.username}`)
                             .then(response => {
                                let resultInfo = response.data;
                                if(resultInfo.success){
                                    this.msg = resultInfo.message;
                                    this.infoColor = "green";
                                    this.countDown(60);
                                }

                              })
                              .catch(error=>{
                                  console.log(error)
                                  alert("服务器忙...");
                              });
                    }else{

                        this.msg = "用户名不能为空";
                        this.infoColor = "red";
                    }
                },
                //按钮的倒计时的效果
                countDown(num){
                    // 设置按钮不可用
                    this.btnDisabled=true;
                    //修改按钮的文本
                    this.btnText=num+"秒后可重新发送";
                    //开启一个定时任务，每一秒钟调用一次
                    var timerId = setInterval(()=>{
                        num--;
                        this.btnText=num+"秒后可重新发送";
                        if(num==0){
                            // 设置按钮可用
                            this.btnDisabled=false;
                            //设置按钮的文本
                            this.btnText="点击重新发送";
                            //取消定时任务
                            clearInterval(timerId);
                        }
                    },1000)
                },
                login(){
                    if(this.user.username && this.user.password && this.user.smsCode){
                        axios.post("user/login",this.user).then(resp=>{
                            //得到resultInfo
                            let resultInfo = resp.data;
                            if(resultInfo.success){
                                //登陆成功
                                location.href="/index.html";
                            }else{
                                //登录失败
                                this.msg = resultInfo.message;
                                this.infoColor="red";
                            }
                        })
                    }else{
                        this.msg = "用户名或密码或验证码不能为空";
                        this.infoColor = "red";
                    }
                }
            }
        })
</script>
</body>
</html>